﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SampleKeyValueDemo.Relationships
{
    public class BackupRelationship<T> : IKeyValueDataStore<T>
    {
        private IKeyValueDataStore<T> backup;
        private IKeyValueDataStore<T> source;
        public BackupRelationship(IKeyValueDataStore<T> source,
            IKeyValueDataStore<T> backup)
        {
            this.backup = backup;
            this.source = source;
        }

        public void Set(string key, T val)
        {
            this.source.Set(key, val);
            this.backup.Set(key, val);
        }

        public T Get(string key)
        {
            T d = this.source.Get(key);
            if (d != null) {
                this.backup.Set(key, d);
                return d; 
            }
            return this.backup.Get(key);
            
        }

        public void Remove(string key)
        {
            this.backup.Remove(key);
            this.source.Remove(key);
        }
    }
}